package swing.swing07;

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SimpleJTree2 {
    JFrame jf=new JFrame("简单树");

    public void init(){

        // 创建DefaultMutableTreeNode对象代表节点
        DefaultMutableTreeNode root=new DefaultMutableTreeNode("中国");
        DefaultMutableTreeNode guangDong=new DefaultMutableTreeNode("广东");
        DefaultMutableTreeNode guangXi=new DefaultMutableTreeNode("广西");
        DefaultMutableTreeNode foShan=new DefaultMutableTreeNode("佛山");
        DefaultMutableTreeNode shanTou=new DefaultMutableTreeNode("汕头");
        DefaultMutableTreeNode guiLin=new DefaultMutableTreeNode("桂林");
        DefaultMutableTreeNode nanNing=new DefaultMutableTreeNode("南宁");

        //组装节点之间的关系
        root.add(guangDong);
        root.add(guangXi);

        guangDong.add(foShan);
        guangDong.add(shanTou);

        guangXi.add(guiLin);
        guangXi.add(nanNing);

        //定义按钮 完成操作
        JButton addSiblingBtn=new JButton("添加兄弟节点");
        JButton addChildBtn=new JButton("添加子节点");
        JButton deleteBtn=new JButton("删除节点");
        JButton editBtn=new JButton("编辑当前节点");

        //创建JTree对象
        JTree tree=new JTree(root);


        //设置节点之间没有连接线
        //tree.putClientProperty("JTree.lineStyle","None");

        //设置节点只有水平分割线
        //tree.putClientProperty("JTree.lineStyle","Horizontal");

        //吧JTree放入到窗口中进行展示
        jf.add(tree);

        //DefaultMutableTreeNode nextNode=guangDong.getNextSibling();
        //System.out.println(nextNode);

        //TreeNode parent=guangDong.getParent();
        //System.out.println(parent);

        //System.out.println(guangDong.isLeaf());
        //System.out.println(foShan.isLeaf());

        //System.out.println(guangDong.isRoot());
        //System.out.println(root.isRoot());

        //完成树的节点编辑的代码
        tree.setEditable(true);
        DefaultTreeModel model = (DefaultTreeModel) tree.getModel();

        //处理添加
        addSiblingBtn.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                //添加兄弟节点逻辑

                //1.获取当前选中的节点
                DefaultMutableTreeNode selectedNode=(DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
                if(selectedNode==null){
                    return;
                }
                //2.获取当前节点的父节点
                DefaultMutableTreeNode parentNode=(DefaultMutableTreeNode) selectedNode.getParent();
                if(parentNode==null){
                    return;
                }
                //3.创建新节点
                DefaultMutableTreeNode newNode=new DefaultMutableTreeNode("新节点");


                //4.把新节点通过父节点进行添加
                int index=parentNode.getIndex(selectedNode);
                model.insertNodeInto(newNode,parentNode,index);

                //5.显示新节点
                TreeNode[] pathToRoot=model.getPathToRoot(newNode);
                TreePath treePath=new TreePath(pathToRoot);
                tree.scrollPathToVisible(treePath);
                //6.重绘tree
                tree.updateUI();
            }
        });

        //添加子节点
        addChildBtn.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                //为选中的节点添加子节点

                //1.获取选中的节点
                DefaultMutableTreeNode selectedNode=(DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
                if(selectedNode==null){
                    return;
                }
                //2.创建新节点
                DefaultMutableTreeNode newNode=new DefaultMutableTreeNode("新节点");

                //3.把新节点添加到当前节点中
                selectedNode.add(newNode);
                //4.显示新节点
                TreeNode[] pathToRoot=model.getPathToRoot(newNode);
                TreePath treePath=new TreePath(pathToRoot);
                tree.scrollPathToVisible(treePath);

                //5.重绘UI
                tree.updateUI();
            }
        });

        //删除节点
        deleteBtn.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                DefaultMutableTreeNode selectedNode=(DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
                if(selectedNode!=null&&selectedNode.getParent()!=null){
                    model.removeNodeFromParent(selectedNode);
                }
            }
        });

        //处理编辑
        editBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               //获取当前选中节点的路径
               TreePath selectionPath=tree.getSelectionPath();

               //判断如果路径不为空 则设置该路径的最后一个节点可编辑
                if(selectionPath!=null){
                    tree.startEditingAtPath(selectionPath);
                }
            }
        });

        jf.add(new JScrollPane(tree));

        JPanel panel=new JPanel();
        panel.add(addSiblingBtn);
        panel.add(addChildBtn);
        panel.add(deleteBtn);
        panel.add(editBtn);

        jf.add(panel, BorderLayout.SOUTH);

        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
        jf.setVisible(true);
    }

    public static void main(String[] args){
        new SimpleJTree2().init();
    }


}
